package basic.thread.again.condition;

/**
 * @author bingo
 * @Date 2018/3/11
 */
public class Demo1 {

    private int  single;

    public synchronized void a(){

        while (single!=0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("a");
        single++;
        notifyAll();
    }
     public synchronized void b (){

        while (single!=1){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
         System.out.println("b");
        single++;
        notifyAll();
        }
    public synchronized void c (){

        while (single!=2){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("c");
        single=0;

    }

    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        T1 t1 = new T1(demo1);
        T2 t2 = new T2(demo1);
        T3 t3 = new T3(demo1);

        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();



    }

}

class T1  implements Runnable{

    private Demo1 demo1;

    public T1(Demo1 demo1) {
        this.demo1 = demo1;
    }

    @Override
    public void run() {

        while (true){
            demo1.a();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

class T2  implements Runnable{

    private Demo1 demo1;

    public T2(Demo1 demo1) {
        this.demo1 = demo1;
    }

    @Override
    public void run() {

        while (true){
            demo1.b();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

class T3  implements Runnable{

    private Demo1 demo1;

    public T3(Demo1 demo1) {
        this.demo1 = demo1;
    }

    @Override
    public void run() {

        while (true){
            demo1.c();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}